﻿/**
 * ...
 * @author 
 */

package list;
import client.INews;
import client.ReceivedItem;
import flash.events.MouseEvent;
import loader.Loader;

class ReceivedItemPanel extends ItemPanel
{
	
	static private inline var IMAGE_HEIGHT   : UInt = 50;
	static private inline var IMAGE_WIDTH    : UInt = 50;
	private static inline var LEFT : Float =  6 + IMAGE_WIDTH;
	
	static private inline var SPACE_FROM_TOP 			: UInt = 3; 	// space before title
	static private inline var SPACE_AFTER_TITLE 		: UInt = 10;
	static private inline var SPACE_AFTER_DESCRIPTION 	: UInt = 8;
	static private inline var SPACE_AFTER_DATE 			: UInt = 8;
	
	private var sender : String;

	public function new(item : INews, index : UInt, sender : String) 
	{
		this.sender = sender;
		super(600, item, index, 1);
	}
	
	private override function initHide(type:Int) {
		hideButton = new MyLinkButton();
		hideButton.setNewText("Удалить");
		hideButton.addEventListener( MouseEvent.CLICK, onHide );
		if(type==0 )
		hideButton.setLocation(LEFT + link.width + 10 + open.width + 10, 	fullNameHeight +
														SPACE_AFTER_TITLE + 
														fullDescriptionHeight + 
														SPACE_AFTER_DESCRIPTION + 
														date.getBounds(date).height + 
														SPACE_AFTER_DATE );
        else	
		hideButton.setLocation(link.x+link.width+10,link.y );
		hideButton.visible = false;
		addChild(hideButton);
	}
	
	private override function onHide(e : MouseEvent) {
		hide();
		cast(parent, NewList).draw();
		var l :Loader = Loader.getInstance();
		l.deleteMessage(cast(content, ReceivedItem).getMessageID());
		e.stopPropagation();
	}
	
	private override function fillShortDescription(textDescription : String) { 
		var text : String = StringTools.htmlUnescape(deleteTags(StringTools.htmlUnescape(textDescription)));
		shortDescription.text = "(Отправил id" + sender + ")   ";
		for (i in 0 ... text.length) {
			if (text.charAt(i) != "\n")
				shortDescription.appendText(text.charAt(i) );
			if (shortDescription.getBounds(shortDescription).width * 1.14 + shortDescription.x  > w ||
				text.charAt(i) == "\n") {
				if( i != text.length - 1) shortDescription.appendText("...");
				return;
			}
		}	
	}
	
	private override function onClick(_) {
		if (mode != DisplayMode.hidden) {
			if ( mode != DisplayMode.selected ) setDisplayMode(DisplayMode.selected);
			else setDisplayMode(DisplayMode.highlighted);
			var item : ReceivedItem = cast(content);
			if (item.is_red() == "no") {
				var l : Loader = Loader.getInstance();
				l.readMessage(item.getMessageID());
			}
			
			cast(parent, NewList).scrollTo(id);
			cast(parent, NewList).draw();
			
		}
	}
	
}